package com.ykg.service.impl;

import com.ykg.dto.EntrustDTO;
import com.ykg.mapper.EntrustMapper;
import com.ykg.pojo.Entrust;
import com.ykg.request.ChangePriceRequest;
import com.ykg.request.CheckEntrustRequest;
import com.ykg.request.UpdateEntrustRequest;
import com.ykg.service.IDTOService;
import com.ykg.service.IEntrustService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Date;
import java.util.Objects;

/**
 * 类(接口)：
 * Created on 2021/6/24 14:49.
 * Author: zetong
 * Description:
 */
@Service
public class EntrustServiceImpl implements IEntrustService {

    @Autowired
    private EntrustMapper entrustMapper;
    @Autowired
    private IDTOService idtoService;


    @Override//查看委托单列表
    public List<Entrust> getEntrust(){
        List<Entrust> entrusts = entrustMapper.findAll();
        return  entrusts;
    }
    @Override//查看委托单详细信息
    public EntrustDTO entrustDetailed(String entrustId){
        Entrust entrust = entrustMapper.findByEntrustId(entrustId);
        EntrustDTO entrustDTO = idtoService.createEntrustDTO(entrust);
        return entrustDTO;
    }
    @Override//生成委托单
    public int generateEntrust(CheckEntrustRequest checkEntrustRequest){
        int result = entrustMapper.generateEntrust(checkEntrustRequest.getEntrustId()
                ,checkEntrustRequest.getReceiver(),(new Date()));
        return result;
    }

    @Override
    /**
    * TODO
    *  更新委托单的状态
    * @params：
    @param updateEntrustRequest
    * @return {@link int}
    * @Author: laobian
    * @Date: 21:40 2021/6/24
    */
    public int updateEntrust(UpdateEntrustRequest updateEntrustRequest) {
        int status = 0;
        if(Objects.isNull(updateEntrustRequest)){
            status = 0;
        }
        int state = entrustMapper.findByEntrustId(updateEntrustRequest.getEntrustId()).getState();
        int end_state = entrustMapper.findByEntrustId(updateEntrustRequest.getEntrustId()).getEndState();
        if(state <=7 && end_state==0){
          status = entrustMapper.updateState(updateEntrustRequest.getEntrustId(),updateEntrustRequest.getState());
        }
        else  if(state==8){
            status = entrustMapper.updateEndState(updateEntrustRequest.getEntrustId(),updateEntrustRequest.getState());
        }

        return status;
    }
}
